﻿using Ninject;
using Ninject.Modules;
using System;

namespace Nx.Kernel
{
    /// <summary>
    /// Nx Kernel is a modified StandardKernel. During the Kernel disposal the Kernel ensures that all modules are disposed as well.
    /// </summary>
    public class NxKernel : StandardKernel
    {
        public NxKernel(params INinjectModule[] modules)
            : base(modules)
        {
        }

        public NxKernel(INinjectSettings settings, params INinjectModule[] modules)
            : base(settings, modules)
        {
        }

        public override void Dispose(bool disposing)
        {
            if (disposing)
            {
                var modules = this.GetModules();
                foreach (var module in modules)
                {
                    this.Unload(module.Name);

                    IDisposable disposable = module as IDisposable;

                    if (disposable != null)
                    {
                        disposable.Dispose();
                        disposable = null;
                    }
                }
            }

            base.Dispose(disposing);
        }
    }
}
